#region
/* Adoor.NET - Object/Relational mapping framework leveraging ADO.NET`s powerful disconnected mode.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
#endregion

using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections.Generic;


namespace Adoor.Expression.AST
{
	public class SelectExpression
	{
		[XmlElement(typeof(ExpressionName))]
		[XmlElement(typeof(ExpressionEscape))]
		[XmlElement(typeof(ExpressionParen))]
		[XmlElement(typeof(ExpressionPrimary))]
		[XmlElement(typeof(ExpressionUnary))]
		[XmlElement(typeof(ExpressionBinary))]
		[XmlElement(typeof(ExpressionIs))]
		[XmlElement(typeof(ExpressionLike))]
		[XmlElement(typeof(ExpressionFunction))]
		public ExpressionNode Expr;

		public string Alias;

		public SelectExpression() // for serialization purposes
		{
		}

		public SelectExpression(ExpressionNode expr, string alias)
		{
            if (expr == null)
                throw new ArgumentNullException("expr");
            if (alias == null)
                throw new ArgumentNullException("alias");
            this.Expr = expr;
			this.Alias = alias;
		}

		public SelectExpression(string alias, ExpressionNode expr) // TODO: remove
		{
            if (alias == null)
                throw new ArgumentNullException("alias");
            if (alias == null)
                throw new ArgumentNullException("alias");
            this.Expr = expr;
			this.Alias = alias;
		}

		public static SelectExpression ParseSelectExpression(Scanner scanner)
		{
			ExpressionParser ep = new ExpressionParser();
			ExpressionNode expr = ep.ParseRowValueConstructor(scanner);
			string alias = "";
			
			if (scanner.Token == ScannerToken.As)
			{
				scanner.Scan();
				if (scanner.Token != ScannerToken.Name)
					throw new Exception("name expected");
			}
			if (scanner.Token == ScannerToken.Name)
			{
				alias = scanner.GetString();
				scanner.Scan();
			}
			return new SelectExpression(expr, alias);
		}

		

		public static SelectExpression[] ParseSelectExpressionList(Scanner scanner)
		{
            List<SelectExpression> list = new List<SelectExpression>();
			do
			{
				scanner.Scan();
				list.Add(SelectExpression.ParseSelectExpression(scanner));
			} while(scanner.Token == ScannerToken.Comma);
			return list.ToArray();
		}

		public static SelectExpression[] ParseSelectExpressionList(string s)
		{
			Scanner scanner = new Scanner(s);
			try
			{
				SelectExpression[] result = SelectExpression.ParseSelectExpressionList(scanner);
				if (scanner.Token != ScannerToken.End)
					throw new Exception("syntax error");
				return result;
			}
			catch(Exception e)
			{
				throw new Exception(e.Message + " (pos = " + scanner.Pos.ToString() + ")", e);
			}
		}
	}
}
